import { _decorator, Component, instantiate, Node, ParticleSystem2D, Prefab, resources, SpriteFrame } from 'cc';
import { heartRoot } from './heartRoot';
const { ccclass, property } = _decorator;

@ccclass('particleRoot')
export class particleRoot extends Component {
    @property(Prefab) LableParticle:Prefab;
    @property(SpriteFrame) lable1:SpriteFrame;
    @property(SpriteFrame) lable2:SpriteFrame;
    @property(SpriteFrame) lable3:SpriteFrame;
    @property(Node) heartRoot:Node;
    imageIndex = 0;
    chi:Node[] = null;
    lables:SpriteFrame[] = [];
    start() 
    {
        this.init()
    }

    init()
    {
        this.lables.push(this.lable1);
        this.lables.push(this.lable2);
        this.lables.push(this.lable3);
        this.chi = this.node.children;
        this.listenDestory();
    }

    listenDestory()
    {
        this.chi[0].on(Node.EventType.NODE_DESTROYED,()=>
        {
            const lable:Node = instantiate(this.LableParticle);
            const particle = lable.getComponent(ParticleSystem2D)
            particle.spriteFrame = this.lables[this.imageIndex];
            particle.startSize = 1000;
            console.log('创建对象')
            this.node.addChild(lable);
            this.imageIndex++;
            if(this.imageIndex < 3)
            {
                this.listenDestory();
            }
            else
            {
                setTimeout(()=>
                {
                    this.heartRoot.getComponent(heartRoot).create();
                },200)
                
            }
        },this)
    }

    update(deltaTime: number) {
        
    }
}


